window.onload=function (){
    findAll()
}
function findAll() {
    axios.get("/user/findAll").then(resp=>{
      var info= resp.data;
        var str ="";
      info.forEach(n=>{
          str+=`<tr><td>${n.userName}</td><td>${n.pwd}</td><td>${n.realName}</td><td>${n.grade}</td><td><input type="button" value="删除用户" onclick="del(${n.id})"  class="button12"><input type="button" value="用户等级修改" onclick="update(${n.id})" class="button12"></td></tr>`;
      })
        $("data").innerHTML=str;
    })
}
var userId;
  function del(userName) {

  axios.get("/user/getLoginUser").then(resp=>{
      var   info= resp.data
           userId= info.id
        console.log(info.id)
    }).then(resp=>{
      if (userId!==userName){
          axios.get("user/delUser",{
              params:{
                  id:userName
              }
          }).then(resp=>{
              findAll()
          })
      }
  })

}
function addUser(){
    console.log($("grade1").value)
    $("info").style.visibility="visible"
}
function affirm(){
    axios.get("/user/checkName",{
        params: {
            userName:$("userTxt").value,
        }
    }).then(resp=>{
        console.log(resp.data)
        if (resp.data==true){
            axios.get("/user/addUser",{
                params:{
                    userName:$("userTxt").value,
                    realName:$("nameTxt").value,
                    grade:$("grade").value
                }
            }).then(findAll,
                $("info").style.visibility="hidden"  )
        }
    })

}
var userId;
function update(id){
    userId=id
    $("info2").style.visibility="visible"
    axios.get("user/findById",{
        params:{
            id:id
        }
    }).then(resp=>{
     var info= resp.data
        $("userSpan").innerHTML=info.userName
        $("nameSpan").innerHTML=info.realName
    })

}
function update1(){
    console.log(userId)

    axios.get("user/updateGrade",{
        params:{
            id:userId,
            level:$("grade1").value
        }
    }).then( resp=>{
        $("info2").style.visibility="hidden",findAll()
    })
}
function cancel(){
    $("info2").style.visibility="hidden"
}
function cancel1(){
    $("info").style.visibility="hidden"
}